//
//  TransactionDetailTableViewController.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/27.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "TransactionDetailTableViewController.h"
#import "TransactionDetailCell.h"
#import "TransSuccessCell.h"

@interface TransactionDetailTableViewController () {
    NSArray *titleArray;
    NSString *receiveStr;
}

@end

@implementation TransactionDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"交易详情"];
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self leftButton:nil];
    
    titleArray = @[@"投资项目",@"投资资金",@"回款时间",@"回款方式"];
    
    receiveStr = [NSString string];
    NSInteger dayCount = _model.day_number+1;
//    NSLog(@"day: %ld day_num: %lf",dayCount,_model.day_number);
    NSDate *nowDate = [NSDate date];
    NSInteger interval = 3600*24*dayCount;
    NSDate *receiveDate = [nowDate dateByAddingTimeInterval:interval];
//    NSLog(@"date: %@",receiveDate);
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    receiveStr = [formatter stringFromDate:receiveDate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!section) {
        return 1;
    } else {
        return 4;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!indexPath.section) {
        return 80;
    } else {
        return 60;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 6;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section) {
        return 60;
    }
    return 1;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section) {
        UIView *footerView = [[UIView alloc] init];
        footerView.backgroundColor = [UIColor clearColor];
        
        UIButton *submitBtn = [[UIButton alloc] init];
        submitBtn.layer.masksToBounds = YES;
        submitBtn.layer.cornerRadius = BTN_RADIUS;
        [submitBtn setTitle:@"确定" forState:UIControlStateNormal];
        [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [submitBtn setBackgroundColor:BLUE_COLOR];
        [submitBtn addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:submitBtn];
        
        [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(footerView.mas_leading).offset(12);
            make.trailing.equalTo(footerView.mas_trailing).offset(-12);
            make.top.equalTo(footerView.mas_top).offset(8);
            make.height.equalTo(@44);
        }];
        return footerView;
    }
    return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView registerNib:[UINib nibWithNibName:@"TransactionDetailCell" bundle:nil] forCellReuseIdentifier:TRANSACTION_CELL];
    [tableView registerNib:[UINib nibWithNibName:@"TransSuccessCell" bundle:nil] forCellReuseIdentifier:TRANSSUC_CELL];
    
    UITableViewCell *cell;
    if (!indexPath.section) {
        TransSuccessCell *sucCell = [TransSuccessCell cellWithTableView:tableView];
        sucCell.order_type = -1;
        [sucCell setUpCell];
        cell = sucCell;
    } else {
        TransactionDetailCell *detailcell = [TransactionDetailCell cellWithTableView:tableView];
        detailcell.cellTitle.text = titleArray[indexPath.row];
        
        switch (indexPath.row) {
            case 0:detailcell.cellSubTitle.text = _model.name;break;
            case 1:detailcell.cellSubTitle.text = [NSString stringWithFormat:@"%@元",_investMoney];break;
            case 2:detailcell.cellSubTitle.text = receiveStr;break;
            case 3:detailcell.cellSubTitle.text = @"到期还本付息，返还到账户余额";break;
                
            default:
                break;
        }
        cell = detailcell;
    }
    return cell;
}

- (void)submitClick {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
